import { defineStore } from 'pinia'
import menuApi from '@/api/menu'

export const useMenuStore = defineStore('menu', {
    state: () => ({
        list: [],
        total: 0,
        loading: false,
        queryParams: {
            page: 1,
            pageSize: 10,
            search: ''
        }
    }),
    actions: {
        async fetchMenus() {
            this.loading = true
            try {
                const res = await menuApi.getList(this.queryParams)
                this.list = res.data.items
                this.total = res.data.total
            } finally {
                this.loading = false
            }
        },
        async createMenu(menuData) {
            await menuApi.create(menuData)
            await this.fetchMenus()
        },
        async updateMenu(id, menuData) {
            await menuApi.update(id, menuData)
            await this.fetchMenus()
        },
        async deleteMenu(id) {
            await menuApi.delete(id)
            await this.fetchMenus()
        }
    }
})